package com.example.typetest;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;



public class MainActivity2 extends Activity {
	
    private AsyncTask<Integer, Integer, Long>    XmlParser_Asynctask = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        XmlParser_Asynctask = new XmlParserAsynctask().execute(0);

        
    }
    
    
    private class XmlParserAsynctask extends AsyncTask<Integer, Integer, Long> {
    	HttpURLConnection urlConnection = null;
    	
		@Override
		protected Long doInBackground(Integer... params) {
	        try {
	        	URL url = new URL("http://m.88models.com/88models/connectDB/index.php");
	        	urlConnection = (HttpURLConnection) url.openConnection();
	        	InputStream in = new BufferedInputStream(urlConnection.getInputStream());
	            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	            DocumentBuilder db = dbf.newDocumentBuilder();
	            Document doc = db.parse(new InputSource(in));
	            doc.getDocumentElement().normalize();

	            NodeList nodeList = doc.getElementsByTagName("mem");
	            Log.e("MainActivity2", "nodeList.getLength : " + nodeList.getLength());
	            
	            for (int i = 0; i < nodeList.getLength(); i++) {
	                Log.e("MainActivity2", "i : " + i);
	                Node node = nodeList.item(i);

	                Element fstElmnt = (Element) node;
	                NodeList idList = fstElmnt.getElementsByTagName("id");
	                Element idElement = (Element) idList.item(0);
	                idList = idElement.getChildNodes();
	                Log.e("MainActivity2", "id : " + ((Node) idList.item(0)).getNodeValue());

	                NodeList phoneList = fstElmnt.getElementsByTagName("phone");
	                Element phoneElement = (Element) phoneList.item(0);
	                phoneList = phoneElement.getChildNodes();
	                Log.e("MainActivity2", "phone : " + ((Node) phoneList.item(0)).getNodeValue());
	                
	                NodeList keyboardList = fstElmnt.getElementsByTagName("keyboard");
	                Element keyboardElement = (Element) keyboardList.item(0);
	                keyboardList = keyboardElement.getChildNodes();
	                Log.e("MainActivity2", "keyboard : " + ((Node) keyboardList.item(0)).getNodeValue());
	                
	                NodeList pointList = fstElmnt.getElementsByTagName("point");
	                Element pointElement = (Element) pointList.item(0);
	                pointList = pointElement.getChildNodes();
	                Log.e("MainActivity2", "point : " + ((Node) pointList.item(0)).getNodeValue());
	            }
	        } catch (Exception e) {
	            Log.e("MainActivity2", "XML Pasing Excpetion = " + e);
	        } finally {
	        	urlConnection.disconnect();
	        }
			return null;
		}
    	
    }
}
